23 #define D(x) cout << #x " = " << (x) << endl
25 typedef unsigned long long Int
;
26 const int BUFSIZE
= 256;
34 bool end
; //is this node a complete prefix?
35 node(bool e
) : end(e
) {
40 //not used, because I love to waste memory (makes me feel sexy)
42 if (n
== NULL
) return;
43 clean(n
->left
); clean(n
->right
);
47 Int
f(string s
, node
* n
){ //s = string built so far, n = the node we are standing at
48 Int left
= 0, right
= 0, ret
= 0;
50 left
= f(s
+ "0", n
->left
);
52 if (n
->right
!= NULL
){
53 right
= f(s
+ "1", n
->right
);
59 if (M
- s
.size() < 64){ //we are safe from overflow
60 Int x
= ((1ULL) << (M
- s
.size())); //2^(m - |s|)
61 howmany
= x
- left
- right
;
62 }else{ //2^64 overflows!
64 x
= ~x
; // (x = ~0ULL) <-> (x = 2^64 - 1)
65 howmany
= x
- left
- right
+ 1;
75 while (scanf("%d %d", &n
, &M
)==2 && !(n
== 0 && M
== 0)){
76 assert(getchar() == '\n');
81 node
* root
= new node(true);
83 fgets(buf
, BUFSIZE
, stdin
);
84 assert(buf
[0] != '*');
92 if (cur
->left
== NULL
) cur
->left
= new node(false);
94 }else if (buf
[i
] == '1'){
95 if (cur
->right
== NULL
) cur
->right
= new node(false);
105 assert(getchar() == '\n');
107 fgets(buf
, BUFSIZE
, stdin
);
108 buf
[strlen(buf
)-2] = '\0'; //delete *\n
109 Int t
= ans
[string(buf
)];
114 assert(getchar() == '\n'); //empty line